package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class PrimerServlet
 */
public class RequestInfo extends ServletMaestro {
       
	private static final long serialVersionUID = -7222970621860173660L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public RequestInfo() {
        super();
     
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		
			response.setContentType("text/html");
	        PrintWriter out = response.getWriter();
	        out.println("<html>");
	        out.println("<body>");
	    	out.println("<form action='index.jsp'><input type='submit' value='Volver a index'/></form>");
	        out.println("<head>");
	        out.println("<title>Request Information</title>");
	        out.println("</head>");
	        out.println("<body>");
	        out.println("<h3>Request Information Example</h3>");
	        out.println("Method: " + request.getMethod() +"<br/>");
	        out.println("Request URI: " + request.getRequestURI() +"<br/>");
	        out.println("Protocol: " + request.getProtocol() +"<br/>");
	        out.println("PathInfo: " + request.getPathInfo() +"<br/>");
	        out.println("Remote Address: " + request.getRemoteAddr() +"<br/>");
	        out.println("Remote Port: " + request.getRemotePort() +"<br/>");
	        out.println("Character Encoding: " + request.getCharacterEncoding() +"<br/>");
	        out.println("Server Name: " + request.getServerName() +"<br/>");
	        out.println("Server Port: " + request.getServerPort() +"<br/>");
	        out.println("Servlet Path: " + request.getServletPath()+"<br/>");
	        out.println("</body>");
	        out.println("</html>");	
	        out.println("<hr>");
	        out.println("<h3>Parametros introducidos por GET</h3>"); out.println("<br/><br/>");
	        out.println("Nombre: " + request.getParameter("nombre"));
	        out.println("<br/>");
	        out.println("Apellido1: " + request.getParameter("apellido1"));
	        out.println("<br/>");
	        out.println("Apellido2: " + request.getParameter("apellido2"));
	        out.println("<br/>");
	        out.println("Sexo: " + request.getParameter("sexo"));
	        out.println("<br/>");
	        out.println("<br/>");
	        out.println("<hr>");
	        out.println("<h3>Parametros Dinamicos</h3>");
	        Enumeration<String> parameterNames = request.getParameterNames();
	        while (parameterNames.hasMoreElements()) {
	    
	        	String paramName = parameterNames.nextElement();
	        	out.println(paramName +": ");
	            
	        	 String[] paramValues = request.getParameterValues(paramName);
	        	for (int i = 0; i < paramValues.length; i++) {
	            	String paramValue = paramValues[i];
	        	    out.println(paramValue + "<br/>");
	            }
	        	 
	        }
	               
	        out.println("<h3>Parametros Cabecera</h3>"); 
	        Enumeration e = request.getHeaderNames();
	        while (e.hasMoreElements()) {
	            String name = (String)e.nextElement();
	            String value = request.getHeader(name);
	            out.println(name + " = " + value +"<br/>");
	        }
	        
	        
	   
	        out.println("<br/>");
	        out.println("<hr>");
	        //out.println("<a href='index.jsp'>Volver a index</a>");
			out.println("<form action='index.jsp'><input type='submit' value='Volver a index'/></form>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Puenteado las peticiones POST a doGet
		//doGet(request,response);
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
      	out.println("<form action='index.jsp'><input type='submit' value='Volver a index'/></form>");
        out.println("<head>");
        out.println("<title>Request Information Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Request Information Example</h3>");
        out.println("Method: " + request.getMethod() +"<br/>");
        out.println("Request URI: " + request.getRequestURI() +"<br/>");
        out.println("Protocol: " + request.getProtocol() +"<br/>");
        out.println("PathInfo: " + request.getPathInfo() +"<br/>");
        out.println("Remote Address: " + request.getRemoteAddr() +"<br/>");
        out.println("Remote Port: " + request.getRemotePort() +"<br/>");
        out.println("Character Encoding: " + request.getCharacterEncoding() +"<br/>");
        out.println("Server Name: " + request.getServerName() +"<br/>");
        out.println("Server Port: " + request.getServerPort() +"<br/>");
        out.println("Servlet Path: " + request.getServletPath()+"<br/>");
        out.println("</body>");
        out.println("</html>");	
        out.println("<br/>");
        out.println("<hr>");
        out.println("<h3>Parametros introducidos por POST</h3>"); out.println("<br/>");
        out.println("Nombre: " + request.getParameter("nombre"));
        out.println("<br/>");
        out.println("Apellido1: " + request.getParameter("apellido1"));
        out.println("<br/>");
        out.println("Apellido2: " + request.getParameter("apellido2"));
        out.println("<br/>");
        if(request.getParameter("radio-sexo")!=null){
        	if(request.getParameter("radio-sexo").equals("hombre")){
        		out.println("Sexo: Hombre");
        	}else if(request.getParameter("radio-sexo").equals("mujer")){
        		out.println("Sexo: Mujer");
        	}
        }
        out.println("<h3>Parametros Dinamicos</h3>"); out.println("<br/>");
       
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
    
        	String paramName = parameterNames.nextElement();
        	out.println(paramName +": ");
            
        	 String[] paramValues = request.getParameterValues(paramName);
        	for (int i = 0; i < paramValues.length; i++) {
            	String paramValue = paramValues[i];
        	    out.println(paramValue + "<br/>");
            }
        	 
        }
        
        
        out.println("<h3>Parametros Cabecera</h3>"); out.println("<br/>");
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            String value = request.getHeader(name);
            out.println(name + " = " + value +"<br/>");
        }

        
        out.println("<h3>Variables de Session</h3>"); 
        
        //Guardar session
        HttpSession session = request.getSession();
        session.setAttribute("user", request.getParameter("nombre"));

        // print session info

        Date created = new Date(session.getCreationTime());
        Date accessed = new Date(session.getLastAccessedTime());
        out.println("ID " + session.getId());
        out.println("Created: " + created);
        out.println("Last Accessed: " + accessed);
        
        
        
        out.println("<br/>");
        out.println("<hr>");
       // out.println("<li><a href='index.jsp'>Volver a index</a></li>");
    	out.println("<form action='index.jsp'><input type='submit' value='Volver a index'/></form>");
	}

}
